package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.pojo.dto.AddressAddNewDTO;
import cn.tedu.fruitshop.pojo.vo.AddressListItemVO;
import cn.tedu.fruitshop.pojo.vo.AddressStandardVO;

import cn.tedu.fruitshop.service.AddressService;
import cn.tedu.fruitshop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author www
 * @create 2022-11-05 17:52
 */
@Slf4j
@Valid
@Api(tags = "06.地址管理模块")
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    //http:localhost:8080/address/add-new
    @ApiOperation("添加地址")
    @ApiOperationSupport(order = 101)
    @PostMapping("/add-new")
    public JsonResult insert(@RequestBody AddressAddNewDTO addressAddNewDTO) {
        log.debug("开始处理【添加地址的请求】，参数：{}",addressAddNewDTO);
        addressService.add(addressAddNewDTO);
        return JsonResult.ok();
    }

    //http:localhost:8080/address/location/delete
    @ApiOperation("根据地址名称删除地址")
    @ApiOperationSupport(order = 201)
    @ApiImplicitParam(name = "location", value = "地址名称", required = true, dataType = "String")
    @PostMapping("/{location}/delete")
    public JsonResult<Void> deleteByTitle(@NotNull(message = "地址名称不能为空") @PathVariable String location) {
        log.debug("开始处理【根据地址名称删除地址】的请求，参数：{}", location);
        addressService.deleteByLocation(location);
        return JsonResult.ok();
    }



    @ApiOperation("根据id删除地址")
    @ApiOperationSupport(order = 202)
    @ApiImplicitParam(name = "id", value = "地址id", required = true, dataType = "String")
    @GetMapping("/{id:[0-9]+}/deleteById")
    public JsonResult<Void> deleteById(@PathVariable Integer id) {
        log.debug("开始处理【根据地址id删除地址】的请求，参数：{}", id);
        addressService.deleteById(id);
        return JsonResult.ok();
    }


    // http://localhost:8080/address
    @ApiOperation("根据用户id查地址列表")
    @ApiOperationSupport(order = 401)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<List<AddressListItemVO>> list(@PathVariable Integer id) {
        log.debug("开始处理【查询地址列表】的请求，无参数");
        List<AddressListItemVO> list = addressService.list(id);
        log.info("地址列表{}", list);
        return JsonResult.ok(list);
    }

    @ApiOperation("修改地址")
    @ApiOperationSupport(order = 201)
    @PostMapping("/update")
    public JsonResult<Void> updateAddress(@RequestBody AddressAddNewDTO addressAddNewDTO) {
        log.debug("开始处理【修改地址】的请求，参数：{}", addressAddNewDTO);
        addressService.updateAddress(addressAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询地址详情")
    @ApiOperationSupport(order = 500)
    @GetMapping ("/{id:[0-9]+}/selectById")
    public JsonResult<AddressStandardVO> selectById(@PathVariable Integer id) {
        log.debug("开始处理【根据id查询地址详情】的请求，参数：{}", id);
        AddressStandardVO selectById = addressService.selectById(id);
        return JsonResult.ok(selectById);
    }


    @ApiOperation("修改默认地址")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "地址id", required = true, dataType = "int")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Integer id) {
        log.debug("开始处理【修改默认地址】的请求，参数：{}", id);
        addressService.setEnable(id);
        return JsonResult.ok();
    }


    @ApiOperation("修改为非默认地址")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "地址id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Integer id) {
        log.debug("开始处理【修改为非默认地址】的请求，参数：{}", id);
        addressService.setDisable(id);
        return JsonResult.ok();
    }



}
